﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TsCore;
using System.Globalization;

namespace TsWeb.Matches
{
    public partial class ManageMatches : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindMatches();
          
                BindSportsToComboBox();
                BindFixturesToComboBox();
               
            }
        }

        private void BindMatches()
        {
            MatchesGrid.DataSource = TsSystem.Instance.GetAllMatchesWithTeams();
            MatchesGrid.DataBind();
            MatchesGrid.UseAccessibleHeader = true;
            if (MatchesGrid.HeaderRow != null)
            {
                MatchesGrid.HeaderRow.TableSection = TableRowSection.TableHeader;
            }         
        }

       



        private void BindSportsToComboBox()
        {
            List<Sport> sportList = TsSystem.Instance.GetAllSports();
            ComboBoxSport.DataSource = sportList;
            ComboBoxFixtureSport.DataSource = sportList;
            ComboBoxSport.DataBind();
            ComboBoxFixtureSport.DataBind();
            if (ComboBoxSport.Items.Count > 0)
            {
                ComboBoxSport.SelectedIndex = 0;
                ComboBoxFixtureSport.SelectedIndex = 0;
                ComboBoxSport_SelectedIndexChanged(null, null);
            }
            dateText.Text = DateTime.Now.AddDays(1).ToShortDateString();
        }
        private void BindFixturesToComboBox()
        {
            DropDownFixtureGenerators.DataSource = TsSystem.Instance.GetAvailableFixtureGenerators();
            DropDownFixtureGenerators.DataBind();
            if (DropDownFixtureGenerators.Items.Count > 0)
            {
                DropDownFixtureGenerators.SelectedIndex = 0;
                DropDownFixtureGenerators_SelectedIndexChanged(null, null);
            }
            FixTextDate.Text = DateTime.Now.AddDays(1).ToShortDateString();
        }
        
        protected void ButtonCreateMatch_Click(object sender, EventArgs e)
        {
            Team home = TsSystem.Instance.GetTeam(Int32.Parse(DDL1_teamHome.SelectedItem.Value));
            Team visitor = TsSystem.Instance.GetTeamWithoutIncludes(Int32.Parse(DDL2_teamVisitor.SelectedItem.Value));
            DateTime date = System.DateTime.Now;
            if (dateText.Text != "")
            {
                date = Convert.ToDateTime(dateText.Text, CultureInfo.CurrentCulture);
                
            }
            //Creo el match
            TsSystem.Instance.CreateNewMatch(date, home, visitor);
            BindMatches();


        }
        protected void ButtonCreateMatchModal_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "call me", "$('#newMatchModal').modal('show');", true);
        }
        




        
        protected void MatchesGrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = MatchesGrid.Rows[index];
            int MatchKey = Convert.ToInt32(row.Cells[0].Text);
            
            if (e.CommandName == "EditCommand")
            {

               
                Response.Redirect("~/Matches/EditMatch.aspx?MatchId=" + MatchKey.ToString());
                
              
              

            }
            if (e.CommandName == "DeleteCommand")
            {
                //TODO: Hacer que muestre una notificacion o algo cuando borra.
                Match match = TsSystem.Instance.GetMatch(MatchKey);
                TsSystem.Instance.RemoveMatch(match);
            }
        }

  

        protected void ComboBoxSport_SelectedIndexChanged(object sender, EventArgs e)
        {
               Int32 sport = Int32.Parse(ComboBoxSport.SelectedItem.Value);
               DDL1_teamHome.DataSource = TsSystem.Instance.GetTeamsForSport(sport);
               DDL1_teamHome.DataBind();
               DDL2_teamVisitor.DataSource = TsSystem.Instance.GetTeamsForSport(sport);
               DDL2_teamVisitor.DataBind();
        }

        protected void DropDownFixtureGenerators_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 FixtureId = Int32.Parse(DropDownFixtureGenerators.SelectedItem.Value);
            FixtureDesc.Text = TsSystem.Instance.GetFixtureById(FixtureId).Description;

        }

        protected void ButtonCreateAutoMatch_Click(object sender, EventArgs e)
        {
            Int32 FixtureId = Int32.Parse(DropDownFixtureGenerators.SelectedItem.Value);
            Int32 sport = Int32.Parse(ComboBoxFixtureSport.SelectedItem.Value);
            Sport selectedSport = TsSystem.Instance.GetSportWithTeams(sport);
            Fixture selectedFixture = TsSystem.Instance.GetFixtureById(FixtureId);
            DateTime date = DateTime.Now;
            if (FixTextDate.Text != "")
            {
                date = Convert.ToDateTime(FixTextDate.Text, CultureInfo.CurrentCulture);

            }
            TsSystem.Instance.AutoGenerateMatches(date, selectedFixture, selectedSport.Teams);
            BindMatches();
        }

        protected void MatchesGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    int winner = Int32.Parse(e.Row.Cells[4].Text);
                    
                    if (winner == -1)
                        e.Row.Cells[4].Text = "Partido sin jugar";
                    if (winner == 0)
                        e.Row.Cells[4].Text = "Empate";
                    if (winner != -1 && winner != 0)
                    {
                        e.Row.Cells[4].Text = TsSystem.Instance.GetTeamWithoutIncludes(winner).Name;
                    }


                }
            }
            catch (Exception)
            { }

        }
    }
}
